var app = getApp();
var adds = {};
var utils = require("../../../utils/util.js");
var timer = null;
const recorderManager = wx.getRecorderManager();

recorderManager.onResume(() => {
  console.log('recorder resume');
})
recorderManager.onPause(() => {
  console.log('recorder pause');
})

recorderManager.onFrameRecorded((res) => {
  const { frameBuffer } = res
  console.log('frameBuffer.byteLength', frameBuffer.byteLength);
})



Page({
  data: {
    voiceArr: [],
    formdata: '',
    recorderOptions: {
      duration: 10000,
      sampleRate: 44100,
      numberOfChannels: 1,
      encodeBitRate: 192000,
      format: 'aac',
      frameSize: 50
    },
    aaa: 0,
    recorderData: {
      tempFilePath: '',
      recorderTime: '00:00:00',
      millisecond: 0,
      second: 0,
      minute: 0,
      totalMs: 0, //总毫秒
    }
  },
  startRecorderBtn: function () {  
    recorderManager.start(this.data.recorderOptions);
    recorderManager.onStart(() => {
      console.log('recorder start');
      var that = this;
      // timer = setInterval(function () {
      //   that.setData({
      //     recorderData: {
      //       totalMs: that.data.recorderData.totalMs + 10
      //     }
      //   });
      //   var totalMs = that.data.recorderData.totalMs;        
      //   var ms = parseInt(totalMs % 60);         
      //   var s = parseInt(totalMs % 60 / 60);
      //   var m = parseInt(totalMs % 3600 / 60);
      //   var timeStr = that.toDub(m) + ":" + that.toDub(s) + ":" + that.toDub(ms);
      //   that.data.recorderData.recorderTime = timeStr;
      //   that.setData({
      //     recorderData: {
      //       recorderTime: timeStr
      //     }
      //   });
      //   console.log(that.data.recorderData.recorderTime );
      // }, 10);
    })
  },
  stopRecorderBtn: function () {
    clearInterval(timer);
    recorderManager.onStop((res) => {
      console.log('recorder stop', res);
      this.data.recorderData.tempFilePath = res.tempFilePath;
      console.log("log------" + this.data.recorderData.tempFilePath);
      recorderManager.onFrameRecorded((res) => {
        debugger;
        const { frameBuffer } = res
        console.log('frameBuffer.byteLength', frameBuffer.byteLength);
      })
    });
  },
  //补零
  toDub: function (n){
    return n< 10 ? "0" + n : "" + n;
  },
  uploadRecorderFile: function(){
    var that = this;
    wx.uploadFile({
      url: utils.commonAPI.UPLOAD_VOICE_URL,
      filePath: that.data.recorderData.tempFilePath,
      method: 'post',
      name: 'file',
      header: {
        "Content-Type":
        "multipart/form-data"
      },
      success: function (res) {
        console.log(res)
        if (res) {
          wx.showToast({
            title: '上传成功！',
          });
        }
      },
      fail: function () {
        wx.showToast({
          title: '上传失败！',
        })
      }
    })
  },
  resetTimer: function () {
    this.data.recorderData.recorderTime = "00:00:00"; 
    this.data.recorderData.millisecond = 0; 
    this.data.recorderData.second = 0; 
    this.data.recorderData.minute = 0; 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})